package com.itx.gatewayx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itx.gatewayx.common.R;
import com.itx.gatewayx.domain.ChatMessage;
import com.itx.gatewayx.domain.ChatSession;
import com.itx.gatewayx.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天控制器
 *
 * @author itx
 */
@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
public class ChatController {
    
    private final ChatService chatService;
    
    /**
     * 获取用户的聊天会话列表
     *
     * @param userId 用户ID
     * @return 会话列表
     */
    @GetMapping("/sessions/{userId}")
    public R<List<ChatSession>> getUserSessions(@PathVariable Long userId) {
        List<ChatSession> sessions = chatService.getUserSessions(userId);
        return R.ok(sessions);
    }
    
    /**
     * 获取或创建与指定用户的聊天会话
     *
     * @param userId 用户ID
     * @param targetUserId 目标用户ID
     * @return 会话
     */
    @PostMapping("/session")
    public R<ChatSession> getOrCreateSession(
            @RequestParam("userId") Long userId,
            @RequestParam("targetUserId") Long targetUserId) {
        ChatSession session = chatService.getOrCreateSession(userId, targetUserId);
        return R.ok(session);
    }
    
    /**
     * 分页获取聊天记录
     *
     * @param current 当前页
     * @param size 每页大小
     * @param chatId 会话ID
     * @return 聊天记录
     */
    @GetMapping("/messages")
    public R<IPage<ChatMessage>> getChatMessages(
            @RequestParam(value = "current", defaultValue = "1") Long current,
            @RequestParam(value = "size", defaultValue = "20") Long size,
            @RequestParam("chatId") Long chatId) {
        IPage<ChatMessage> messagePage = chatService.getChatMessages(current, size, chatId);
        return R.ok(messagePage);
    }
    
    /**
     * 发送消息
     *
     * @param message 消息
     * @return 是否成功
     */
    @PostMapping("/message")
    public R<Boolean> sendMessage(@RequestBody @Validated ChatMessage message) {
        boolean result = chatService.sendMessage(message);
        return result ? R.ok(true) : R.fail("发送消息失败");
    }
    
    /**
     * 将消息标记为已读
     *
     * @param chatId 会话ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @PutMapping("/read")
    public R<Boolean> markMessagesAsRead(
            @RequestParam("chatId") Long chatId,
            @RequestParam("userId") Long userId) {
        boolean result = chatService.markMessagesAsRead(chatId, userId);
        return result ? R.ok(true) : R.fail("标记已读失败");
    }
    
    /**
     * 获取未读消息数
     *
     * @param chatId 会话ID
     * @param userId 用户ID
     * @return 未读消息数
     */
    @GetMapping("/unread/count")
    public R<Integer> getUnreadCount(
            @RequestParam("chatId") Long chatId,
            @RequestParam("userId") Long userId) {
        int count = chatService.getUnreadCount(chatId, userId);
        return R.ok(count);
    }
    
    /**
     * 获取用户所有会话的未读消息总数
     *
     * @param userId 用户ID
     * @return 未读消息总数
     */
    @GetMapping("/unread/total/{userId}")
    public R<Integer> getTotalUnreadCount(@PathVariable Long userId) {
        int count = chatService.getTotalUnreadCount(userId);
        return R.ok(count);
    }
} 